﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ColorMix
{
    public partial class Form1 : Form
    {
        public OpenFileDialog TextureDialog;
        private Bitmap diffuse;
        private string diffpath;
        private Bitmap normal;
        private Bitmap specularpower;
        private Bitmap specularintensity;

        private Bitmap difresult;
        private Bitmap normresult;

        public Form1()
        {
            TextureDialog = new OpenFileDialog();
            TextureDialog.Filter = "24/32bit Images (*.jpg; *.png)|*.jpg; *.png";
            TextureDialog.Multiselect = false;

            InitializeComponent();
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();  
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //bool? clicked = TextureDialog.ShowDialog();
            if (TextureDialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = TextureDialog.FileName;
                diffuse = new Bitmap(TextureDialog.FileName);
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (TextureDialog.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = TextureDialog.FileName;
                normal = new Bitmap(TextureDialog.FileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (TextureDialog.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = TextureDialog.FileName;
                specularpower = new Bitmap(TextureDialog.FileName);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (TextureDialog.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = TextureDialog.FileName;
                specularintensity = new Bitmap(TextureDialog.FileName);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bool diffok = false;
            bool normok = false;

            if (diffuse == null  ||  specularpower == null)
            {
                DialogResult msg = MessageBox.Show("Falta la textura Difuso o la Oclusión!", "Error al procesar", MessageBoxButtons.OK);
                return;
            }
            if (normal == null || specularintensity == null)
            {
                DialogResult msg = MessageBox.Show("Falta la textura Normal o la Especular!", "Error al procesar", MessageBoxButtons.OK);
                return;
            }
            if (diffuse.Width != specularpower.Width || diffuse.Height != specularpower.Height)
            {
                DialogResult msg = MessageBox.Show("Texturas Difuso y Oclusión deben tener el mismo tamaño", "Error al procesar", MessageBoxButtons.OK);
                return;
            }
            if (normal.Width != specularintensity.Width || normal.Height != specularintensity.Height)
            {
                DialogResult msg = MessageBox.Show("Texturas Difuso y Oclusión deben tener el mismo tamaño", "Error al procesar", MessageBoxButtons.OK);
                return;
            }
            int percent = diffuse.Width * diffuse.Height;
            string windowtitle = this.Text;
            if (diffuse != null && specularpower != null)
            {
                difresult = new Bitmap(diffuse.Width, diffuse.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                for (int x = 0; x < diffuse.Width; x++)
                    for (int y = 0; y < diffuse.Height; y++)
                    {
                        Color d = new Color();
                        d = Color.FromArgb(specularpower.GetPixel(x, y).R, diffuse.GetPixel(x, y).R,
                            diffuse.GetPixel(x, y).G, diffuse.GetPixel(x, y).B);
                        difresult.SetPixel(x, y, d);
                        this.Text = "Procesado..." + ((x + y) * 100) / percent + "%";
                    }
                diffok = true;
            }
            this.Text = windowtitle;
            percent = normal.Width*normal.Height;
            if (normal != null && specularintensity != null)
            {
                normresult = new Bitmap(normal.Width, normal.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                for (int x = 0; x < normal.Width; x++)
                    for (int y = 0; y < normal.Height; y++)
                    {
                        Color d = new Color();
                        d = Color.FromArgb(specularintensity.GetPixel(x, y).R, normal.GetPixel(x, y).R,
                            normal.GetPixel(x, y).G, normal.GetPixel(x, y).B);
                        normresult.SetPixel(x, y, d);
                        this.Text = "Procesado..." + ((x + y) * 100) / percent + "%";

                    }
                normok = true;
            }
            this.Text = windowtitle;

            if (diffok)
            {
                Form2 frm = new Form2(difresult, "Difuso Combinado");
                frm.Show();
            }
            if (normok)
            {
                Form2 frm = new Form2(normresult, "Normal Combinado");
                frm.Show();
            }
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DialogResult msgbox = MessageBox.Show("Lluis Sanahuja, 2014. Licencia CC SA-BY\n ldsanahuja@gmail.com", "Acerca de...", MessageBoxButtons.OK);
        }
    }
}
